﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>
    $(function () {
        var articleTypes =  @Html.Raw(ViewBag.ArticleType) ;
        top.clients.dataItems["ArticleType"] = articleTypes ;
        $("#sel_type").html();
        for (var i = 0; i < articleTypes.length; i++) {
            var at = articleTypes[i];
            $("#sel_type").append("<option value='"+at.Id+"'>"+at.Name+"</option>");
        }
        $("#sel_type").val(3);
        top.clients.dataItems["ArticleState"] = { "-1": "所有", 0: "未发布", 1: "已发布", 2: "失效" };
        gridList();
    });
    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/Article/Article/GetGridJson",
            height: $(window).height() - 128,
            postData: {
                keyword: $("#txt_keyword").val(),
                type: 3,
                state: -1
            },
            colModel: [
                { label: "主键", name: "F_Id", hidden: true, key: true },
                { label: '标题', name: 'F_Title', width: 250, align: 'left' },
                {
                    label: '类型', name: 'F_Type', width: 80, align: 'left',
                    formatter: function (cellvalue) {
                        var types = top.clients.dataItems["ArticleType"];
                        for (var i = 0; i < types.length; i++) {
                            var type = types[i];
                            if (type.Id == cellvalue) {
                                return type.Name;
                            }
                        }
                        return "-";
                    }
                },
                { label: '点击量', name: 'F_ClickNumber', width: 50, align: 'left' },
                { label: '评论量', name: 'F_CommentNumber', width: 50, align: 'left' },
                {
                    label: '可评论', name: 'F_CanComment', width: 80, align: 'left',
                    formatter: function (cellvalue) {
                        if (cellvalue) {
                            return '<span class=\"label label-success\">是</span>';
                        } else {
                            return '<span class=\"label label-default\">否</span>';
                        }
                    }
                },
                {
                    label: '是否置顶', name: 'F_IsTop', width: 80, align: 'left',
                    formatter: function (cellvalue) {
                        if (cellvalue) {
                            return '<span class=\"label label-success\">是</span>';
                        } else {
                            return '<span class=\"label label-default\">否</span>';
                        }
                    }
                },
                {
                    label: '创建时间', name: 'F_CreatorTime', width: 150, align: 'left',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' }
                },
                {
                    label: '发布时间', name: 'F_ReleaseTime', width: 150, align: 'left',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' }
                },
                {
                    label: '发布状态', name: 'F_State', width: 150, align: 'left',
                    formatter: function (cellvalue) {
                        if (cellvalue == 0) {
                            return '<span class=\"label label-default\">未发布</span>';
                        } else if (cellvalue == 1) {
                            return '<span class=\"label label-success\">发布</span>';
                        } else if (cellvalue == 2) {
                            return '<span class=\"label label-warning\">失效</span>';
                        }
                    }
                }
            ],
            pager: "#gridPager",
            jsonReader: {
                root: "data",
                total: "total",
                records: "records",
                repeatitems: false
            },
            sortname: 'F_CreatorTime desc',
            viewrecords: true
        });
        $("#btn_search").click(function () {
            search();
        });
        $("#sel_type").change(function () {
            search();
        });
        $("#sel_state").change(function () {
            search();
        });
    }
    function search() {
        $("#gridList").jqGrid('setGridParam', {
            postData: {
                keyword: $("#txt_keyword").val(),
                type: $("#sel_type").val(),
                state: $("#sel_state").val()
            },
        }).trigger('reloadGrid');
    }
    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "新增文章",
            url: "/Article/Article/Form",
            width: "800px",
            height: "800px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_edit() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        $.modalOpen({
            id: "Form",
            title: "修改文章",
            url: "/Article/Article/Form?keyValue=" + keyValue,
            width: "800px",
            height: "800px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_delete() {
        $.deleteForm({
            url: "/Article/Article/DeleteForm",
            param: { keyValue: $("#gridList").jqGridRowValue().F_Id },
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        });
    }
    function btn_details() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        $.modalOpen({
            id: "Details",
            title: "查看文章",
            url: "/Article/Article/Details?keyValue=" + keyValue,
            width: "1000px",
            height: "1000px",
            btn: null,
        });
    }
    function btn_audit(state) {
        $.deleteForm({
            prompt: "注：您确定要修改该项数据吗？",
            loading: "正在修改数据...",
            url: "/Article/Article/Audit",
            param: {
                keyValue: $("#gridList").jqGridRowValue().F_Id,
                state: state
            },
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        });
    }
    function btn_candidates() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        var url = "/Article/Comment/IndexSon?pid=" + keyValue;
        var title = "查看评论";
        var frameId = window.frameElement && window.frameElement.id || '';
        frameId = frameId.substring(6);
        parent.addTap2(frameId, url, title);
    }
</script>

<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
        <div class="btn-group">
            <a id="NF-add" authorize="yes" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-plus"></i>新建文章</a>
        </div>
        <div class="operate">
            <ul class="nav nav-pills">
                <li class="first">已选中<span>1</span>项</li>
                <li><a id="NF-edit" authorize="yes" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改文章</a></li>
                <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除文章</a></li>
                <li><a id="NF-audit1" authorize="yes" onclick="btn_audit(1)"><i class="fa fa-check"></i>发布</a></li>
                <li><a id="NF-audit2" authorize="yes" onclick="btn_audit(2)"><i class="fa fa-times"></i>失效</a></li>
                <li><a id="NF-Details" authorize="yes" onclick="btn_details()"><i class="fa fa-search-plus"></i>查看文章</a></li>
                <li><a id="NF-Candidates" authorize="yes" onclick="btn_candidates()"><i class="fa fa-search-plus"></i>查看评论</a></li>
            </ul>
            <a href="javascript:;" class="close"></a>
        </div>
        <script>$('.toolbar').authorizeButton()</script>
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="标题/内容" style="width: 200px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                        <select class="form-control" id="sel_type" style="margin-left:10px;width:120px;">
                        </select>
                        <select class="form-control" id="sel_state" style="margin-left: 10px; width: 120px;">
                            <option value="-1">所有状态</option>
                            <option value="0">未发布</option>
                            <option value="1">已发布</option>
                            <option value="2">失效</option>
                        </select>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>
<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>
